using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using RFS.Domain.PaiShi;
using RFS.Domain.PaiShi.Accounting;
using RFS.Domain.PaiShi.WorkFlow;
using ycx;

namespace RFS.BizLogic.PaiShi.Accounting
{
    public class OrderItemBasedAccountingVoucher : IOrderItemBasedAccountingVoucher
    {
        private IList<IOrderBasedAccItem> _accItems = new List<IOrderBasedAccItem>();

        public override string ToString()
        {
            return $"id:{Id}, Delta：{Balance}:\n{_accItems.Select(x => $"{x}").JoinToString("\n")}";
        }

        public override bool Equals(object obj)
        {
            
            if (obj == null) return false;
            if (obj is IOrderItemBasedAccountingVoucher ot)return ot.Id.Equals(this.Id);
            
            return false;
        }
        

        public OrderItemBasedAccountingVoucher()
        {
            Id = 1.NewSnowflakeId();
        }

        public void AddAccItem(IOrderItem gho, Account acc, string remark, Amount amountDebit = default,
            Amount amountCredit = default)
        {
            var vi = new OrderBasedAccItem();
            vi.Summary = remark;
            vi.Account = acc;
            vi.RelatedOrderItem = gho;
            vi.AmountDebit = amountDebit;
            vi.AmountCredit = amountCredit;
            if (gho is OrderItem oo)
            {
                oo.RecordedInAcc = true;
                // oo.IsChecked = true;
            }
            _accItems.Add(vi);

            
            OnChanged();
        }

        public ulong Id { get; set; }

        public bool IsBalanced
        {
            get => _accItems.Sum(x => x.AmountDebit.AmountCash) == _accItems.Sum(x => x.AmountCredit.AmountCash);
        }

        public Amount Balance
        {
            get => CreditTotal - DebitTotal;
        }

        public Amount DebitTotal
        {
            get => _accItems.Select(x => x.AmountDebit).Sum();
        }

        public Amount CreditTotal
        {
            get => _accItems.Select(x => x.AmountCredit).Sum();
        }

        public IProblem Problem
        {
            get => IsBalanced
                ? new ProblemClass()
                : new ProblemClass($"Fk, [{Id}]不平:[{DebitTotal.AmountCash}=={CreditTotal.AmountCash}]");
        }

        // // public VoucherState State { get; set; }
        //
        // public decimal CumulativeAmountIncurred
        // {
        //     get => _accItems.Sum(x => x.AmountDebit.AmountCash);
        // }
        //
        // public decimal CumulativeQuantityIncurred
        // {
        //     get => AmountIncurred.Quantity;
        // }
        //
        // public Amount AmountIncurred
        // {
        //     get => CreditTotal ;
        // }

        
        
        public IEnumerable<IOrderBasedAccItem> AccItems
        {
            get => _accItems;
            set => _accItems = value.ToList();
        }

        public VType VType { get; set; } = VType.系统生成  ;


        #region changed

        event EventHandler _Changed;

        public event EventHandler Changed
        {
            add
            {
                _Changed += value;
                OnChanged();
            }
            remove => _Changed -= value;
        }

        void OnChanged()
        {
            _Changed?.Invoke(this, EventArgs.Empty);
        }

        #endregion


        public void Remove(IOrderBasedAccItem item)
        {
            var a=_accItems.FirstOrDefault(x=>x.Id==item.Id);
            if (a != null)
            {
            
                _accItems.Remove(a);
                if (a.RelatedOrderItem is OrderItem o)
                {
                    o.RecordedInAcc = false;
                    o.IsChecked = false;
                    a.RelatedOrderItem = null;  
                } 
                OnChanged();
    
            }
            
        }
    }
}